package com.lt.test.provider.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.lt.test.api.KtSaleProjectService;
import com.lt.test.common.util.StringUtils;
import com.lt.test.provider.dao.DCardProdDao;
import com.lt.test.provider.dao.DShop2MemberDao;
import com.lt.test.provider.dao.KtSaleProjectDao;
import com.lt.test.entity.DTO.PageDTO;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(version = "1.0.0")
public class KtSaleProjectServiceImpl implements KtSaleProjectService{
	
	private static final Logger logger = LoggerFactory.getLogger(KtSaleProjectServiceImpl.class);
	@Autowired
	DCardProdDao dCardProdDao;
	
	@Autowired
	DShop2MemberDao dShop2MemberDao;
	
	@Autowired
	KtSaleProjectDao ktSaleProjectDao;
	
	@Override
	public PageDTO<Map<String, Object>> getCardProdListByPage(Map parameter) {
		// TODO Auto-generated method stub
		Integer page = Integer.valueOf(parameter.get("page").toString());
		Integer pageSize = Integer.valueOf(parameter.get("rows").toString());
		String filters = parameter.get("filters") == null ? null : parameter.get("filters").toString();
		Long beginNum = (long) ((page - 1) * pageSize + 1);
		Long endNum = beginNum + pageSize - 1;

		List<Map<String,Object>> infoList = null;
		int recordCount = 0;
		String supplierName = null;
		String helpCode = null;
		
//		if(filters != null && !filters.trim().equals("")) {
//			JSONObject jsonObject = JSONObject.fromObject(filters);
//			JSONArray jsonArray = (JSONArray) jsonObject.get("rules");
//			List<PageFilter> filterList = new ArrayList<>();
//			for(int i = 0; i < jsonArray.size(); i++) {
//				JSONObject result = (JSONObject) jsonArray.get(i);
//				if("supplierName".equals(result.get("field")) && !StringUtils.isNullOrEmpty(result.getString("data"))){
//					supplierName =  result.getString("data");
//				}
//				if("helpCode".equals(result.get("field")) && !StringUtils.isNullOrEmpty(result.getString("data"))){
//					helpCode =  result.getString("data");
//				}
//			}
//		}
		infoList = dCardProdDao.getCardProdListByPage(null, null, null, beginNum, endNum);
		recordCount = Integer.valueOf(dCardProdDao.getCardProdListSize(null, null, null).get("total").toString());
		PageDTO<Map<String,Object>> result = new PageDTO<>(page, pageSize, infoList, recordCount);
		return result;
	}

	@Override
	public void saveCardProd(Map parameter) {
		// TODO Auto-generated method stub
		String id = parameter.get("id") == null ? null : parameter.get("id").toString();
		String type = parameter.get("cardType").toString();
		
		JSONObject obj = new JSONObject();
		obj.put("id", id);
		obj.put("cardName", parameter.get("cardName"));
		obj.put("cardNo", parameter.get("cardNo"));
		obj.put("beginDate", parameter.get("beginTime"));
		obj.put("endDate", parameter.get("endTime"));
		obj.put("minUseAmt", parameter.get("minUseAmt"));
		obj.put("validDays", parameter.get("validDays"));
		if(type.equals("1")){
			//折扣券
			obj.put("discount", parameter.get("cardPrice"));
			obj.put("allProuctsFlag", "1");
			obj.put("productType", "");
			
		}else if (type.equals("2")){
			obj.put("benifitAmt", parameter.get("cardPrice"));
		}else if(type.equals("3")){
			obj.put("productCode", "");
			obj.put("productName", "");
			obj.put("quantity", "");
		}
		
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if(StringUtils.isNullOrEmpty(id)){
			id =  StringUtils.getUUID();
			parameter.put("id", id);
			parameter.put("state", "1");
			parameter.put("operatorTime", new Date());
			try {
				if(!StringUtils.isNullOrEmpty(parameter.get("beginTime"))){
					parameter.put("beginTime", sdf.parse(parameter.get("beginTime").toString()));
				}
				if(!StringUtils.isNullOrEmpty(parameter.get("endTime"))){
					parameter.put("endTime", sdf.parse(parameter.get("endTime").toString()));
				}
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			obj.put("id", id);
			parameter.put("cardData", obj.toString());
			dCardProdDao.create(parameter);
		}else{
			parameter.put("operatorTime", new Date());
			try {
				if(!StringUtils.isNullOrEmpty(parameter.get("beginTime"))){
					parameter.put("beginTime", sdf.parse(parameter.get("beginTime").toString()));
				}
				if(!StringUtils.isNullOrEmpty(parameter.get("endTime"))){
					parameter.put("endTime", sdf.parse(parameter.get("endTime").toString()));
				}
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			parameter.put("cardData", obj.toString());
			dCardProdDao.update(parameter);
		}
	}

	@Override
	public PageDTO<Map<String, Object>> getMemberListByPage(Map parameter) {
		// TODO Auto-generated method stub
		Integer page = Integer.valueOf(parameter.get("page").toString());
		Integer pageSize = Integer.valueOf(parameter.get("rows").toString());
		Long beginNum = (long) ((page - 1) * pageSize + 1);
		Long endNum = beginNum + pageSize - 1;
		List<Map<String,Object>> infoList = null;
		int recordCount = 0;
		try{
			infoList = dShop2MemberDao.getMemberListByPage(parameter, beginNum, endNum);
			recordCount = Integer.valueOf(dShop2MemberDao.getMemberListSize(parameter).get("total").toString());
		}catch(Exception e){
			logger.error(e.getMessage());
		}
		PageDTO<Map<String,Object>> result = new PageDTO<>(page, pageSize, infoList, recordCount);
		return result;
	}

	@Override
	public void saveMember(Map parameter) {
		// TODO Auto-generated method stub
		String id = parameter.get("id") == null ? null : parameter.get("id").toString();
		String userId = parameter.get("operator").toString();
		if(StringUtils.isNullOrEmpty(id)){
			parameter.put("id", StringUtils.getUUID());
			parameter.put("userId", System.currentTimeMillis()/1000);
			parameter.put("createTime", new Date());
			parameter.put("createUser", userId);
			dShop2MemberDao.create(parameter);
		}else{
			parameter.put("modifyTime", new Date());
			parameter.put("modifyUser", userId);
			dShop2MemberDao.update(parameter);
		}
		
	}

	@Override
	public PageDTO<Map<String, Object>> getSaleProjectListByPage(Map parameter) {
		// TODO Auto-generated method stub
		Integer page = Integer.valueOf(parameter.get("page").toString());
		Integer pageSize = Integer.valueOf(parameter.get("rows").toString());
		String filters = parameter.get("filters") == null ? null : parameter.get("filters").toString();
		Long beginNum = (long) ((page - 1) * pageSize + 1);
		Long endNum = beginNum + pageSize - 1;

		List<Map<String,Object>> infoList = null;
		int recordCount = 0;
		String supplierName = null;
		String helpCode = null;
		
//		if(filters != null && !filters.trim().equals("")) {
//			JSONObject jsonObject = JSONObject.fromObject(filters);
//			JSONArray jsonArray = (JSONArray) jsonObject.get("rules");
//			List<PageFilter> filterList = new ArrayList<>();
//			for(int i = 0; i < jsonArray.size(); i++) {
//				JSONObject result = (JSONObject) jsonArray.get(i);
//				if("supplierName".equals(result.get("field")) && !StringUtils.isNullOrEmpty(result.getString("data"))){
//					supplierName =  result.getString("data");
//				}
//				if("helpCode".equals(result.get("field")) && !StringUtils.isNullOrEmpty(result.getString("data"))){
//					helpCode =  result.getString("data");
//				}
//			}
//		}
		infoList = ktSaleProjectDao.getSaleProjectListByPage(null, null, null, beginNum, endNum);
		recordCount = Integer.valueOf(ktSaleProjectDao.getSaleProjectListSize(null, null, null).get("total").toString());
		PageDTO<Map<String,Object>> result = new PageDTO<>(page, pageSize, infoList, recordCount);
		return result;
	}

	@Override
	public void saveSaleProject(Map parameter) {
		// TODO Auto-generated method stub
		String id = parameter.get("id") == null ? null : parameter.get("id").toString();
		if(StringUtils.isNullOrEmpty(id)){
			parameter.put("createTime", new Date());
			parameter.put("state", "1");
			ktSaleProjectDao.create(parameter);
		}else{
			ktSaleProjectDao.update(parameter);
		}
		
	}

}
